package jh.com.jhappbf.unload;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.util.AbDialogUtil;
import com.ab.view.pullview.AbPullToRefreshView;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.utils.JsonHelper;

public class UnloadPlatenumSelect extends AbActivity {
    private View mContentView;
    private View mErrorView;
    private AbPullToRefreshView mRefreshView;
    private ListView mListView;
    private List<JSONObject> mData;
    private myBaseAdapter mAdapter;
    private Button mBtnTryAgain;
    private AbLoadDialogFragment mDialogFragment = null;
    private Context mAppContext;
    private AbTitleBar mAbTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContentView = getLayoutInflater().inflate(R.layout.assay_platenum_select_activity, null);
        mErrorView = getLayoutInflater().inflate(R.layout.net_error_activity, null);
        setAbContentView(mContentView);
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("要卸货的车");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        initView();
    }


    private void initView() {
        mRefreshView = mContentView.findViewById(R.id.assay_select_pullToRefreshView);
        mListView = mContentView.findViewById(R.id.assay_select_platenum_listView);
        mBtnTryAgain = mErrorView.findViewById(R.id.network_error_btn);
        mAppContext = getApplicationContext();

        // 重试
        mBtnTryAgain.setOnClickListener(v -> {
            setAbContentView(mContentView);
            loadFirst();
        });

        // 下拉刷新
        mRefreshView.setOnHeaderRefreshListener(view -> refreshTask());

        // 上拉加载更多
        mRefreshView.setOnFooterLoadListener(view -> refreshTask());

        // 单击数据项
        mListView.setOnItemClickListener((parent, view, position, id) -> {

            JSONObject obj = mData.get(position);
            Intent i = new Intent();
            i.putExtra("result", obj.optString("Platenumber"));
            setResult(0, i);
            UnloadPlatenumSelect.this.finish();
        });

        // 设置进度条的样式
        mRefreshView.getHeaderView().setHeaderProgressBarDrawable(
                ContextCompat.getDrawable(this, R.drawable.progress_circular));
        mRefreshView.getFooterView().setFooterProgressBarDrawable(
                ContextCompat.getDrawable(this, R.drawable.progress_circular));

        mData = new ArrayList<>();
        loadFirst();
        mAdapter = new myBaseAdapter();
        mListView.setAdapter(mAdapter);
    }

    /**
     * 第一次加载
     */
    private void loadFirst() {
        mDialogFragment = AbDialogUtil.showLoadDialog(UnloadPlatenumSelect.this,
                R.mipmap.ic_load, "查询中,请稍等...");
        mDialogFragment.setCancelable(false);
        mDialogFragment.setAbDialogOnLoadListener(this::refreshTask);
    }

    /**
     * 下拉刷新
     */
    private void refreshTask() {
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        String methodName = "getBusinessPlatenumber";

        Listener<String> listener = response -> {

            AbDialogUtil.removeDialog(UnloadPlatenumSelect.this);
            mRefreshView.onHeaderRefreshFinish();
            mRefreshView.onFooterLoadFinish();
            List<JSONObject> newListData = JsonHelper.getJsonObjects(response);
            if (newListData != null) {
                mData = newListData;
                mAdapter.notifyDataSetChanged();
            }
        };

        ErrorListener errorListener = error -> {
            AbDialogUtil.removeDialog(UnloadPlatenumSelect.this);
            mRefreshView.onHeaderRefreshFinish();
            mRefreshView.onFooterLoadFinish();
            setAbContentView(mErrorView);
            mData.clear();
            mAdapter.notifyDataSetChanged();
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(mAppContext).addRequest(webService);
    }

    class myBaseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(UnloadPlatenumSelect.this, R.layout.list_view_item_txt, null);
                holder.txt = convertView.findViewById(R.id.list_view_item_txt);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (holder != null) {
                JSONObject obj = mData.get(position);
                holder.txt.setText(obj.optString("Platenumber") + "   " + obj.optString("Cargoname"));
            }
            return convertView;
        }
    }

    class ViewHolder {
        public TextView txt;
    }
}
